package org.nanotek.lucene.index.search.local;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.lucene.index.Term;
import org.apache.lucene.search.CollectionStatistics;
import org.apache.lucene.search.IndexSearcher;
import org.nanotek.cms.domain.lucene.FieldStat;
import org.nanotek.cms.domain.lucene.Index;
import org.nanotek.domain.lucene.IndexStats;
import org.nanotek.cms.domain.lucene.TermStats;
import org.nanotek.dao.DAO;
import org.nanotek.lucene.index.search.IndexSearcherProvider;
import org.nanotek.lucene.index.search.Provider;
import org.nanotek.lucene.index.search.SearchException;
import org.nanotek.lucene.index.search.SearchService;
import org.nanotek.lucene.query.QueryContext;
import org.nanotek.lucene.query.QueryResult;

public class LocalIndexSearchService implements SearchService {

	/** mpve the PAGE SIZE for a mutable integer "in future"...*/
	private static final Integer PAGE_SIZE = 300;
	private LocalProvider resultsProvider;
	private IndexSearcherProvider searchProvider;
	private DAO<Index> indexDAO;

	public LocalIndexSearchService(){}

	public List<Index> getIndexList()
	{
		return getIndexList (0);
	}

	private List<Index> getIndexList(int i) {
		return indexDAO.listRecords(i, PAGE_SIZE);
	}

	public QueryResult searchIndex(QueryContext queryContext)
	{
		return resultsProvider.search(queryContext);
	}

	public QueryResult searchIndex(String searchPhrase)
	{
		return resultsProvider.search(searchPhrase);
	}

	public QueryResult searchIndex(Index index , String searchPhrase)
	{
		return resultsProvider.search(index, searchPhrase);
	}

	public List<TermStats> getTermStats (Index index, String... terms)
	{
		List<Term> termList = new ArrayList<>();
		for (String term : terms)
		{
			Term luceneTerm =new Term("TextData" , term);
			termList.add(luceneTerm);
		}
		List<TermStats> termStats = searchProvider.getTermStats (index, termList);
		return termStats;
	}

	public org.nanotek.domain.lucene.IndexStats getIndexStats (Index index)
	{
		IndexStats indexStats = new IndexStats();
		IndexSearcher indexSearcher = searchProvider.getIndexSearcher(index);
		try {
			CollectionStatistics stats = indexSearcher.collectionStatistics("TextData");

			indexStats.setSumDocFreq(stats.sumDocFreq());
			indexStats.setStatsFieldCount(stats.docCount());
			indexStats.setSumTotalTermFreq(stats.sumTotalTermFreq());
		}catch (IOException e)
		{
			throw new SearchException(e);
		}
		return indexStats;
	}

	public FieldStat getFieldStat (Index index , String field) throws Exception
	{
		FieldStat stat = searchProvider.getFieldStat(index, field);
		return stat;
	}

	public Provider getResultsProvider() {
		return resultsProvider;
	}

	public void setResultsProvider(LocalProvider resultsProvider) {
		this.resultsProvider = resultsProvider;
	}

	public IndexSearcherProvider getSearchProvider() {
		return searchProvider;
	}

	public void setSearchProvider(IndexSearcherProvider searchProvider) {
		this.searchProvider = searchProvider;
	}

	public DAO<Index> getIndexDAO() {
		return indexDAO;
	}

	public void setIndexDAO(DAO<Index> indexDAO) {
		this.indexDAO = indexDAO;
	}

	public QueryResult search(String searchPhrase) throws Exception {
		assert (resultsProvider !=null);
		return resultsProvider.search(searchPhrase);
	}

	public QueryResult search(Index index, String searchPhrase)
			throws Exception {
		assert (resultsProvider !=null);
		return resultsProvider.search(index, searchPhrase);
	}

	public QueryResult search(QueryContext queryContext) throws Exception {
		assert (resultsProvider !=null);
		return resultsProvider.search(queryContext);
	}

}
